package com.gw.quotes.activity.fragment;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gw.quotes.MyApplication;
import com.gw.quotes.R;
import com.gw.quotes.activity.ChangePassActivity;
import com.gw.quotes.activity.LoginActivity;
import com.gw.quotes.activity.MineEvaluationActivity;
import com.gw.quotes.util.CommonTools;
import com.gw.quotes.util.DialogUtils;
import com.gw.quotes.util.DialogUtils.DialogCallBack;

public class MineFragment extends BaseMainFragment {

	private View contextView = null;

	@ViewInject(id = R.id.login_layout, click = "login")
	private RelativeLayout login;
	@ViewInject(id = R.id.evaluation_layout, click = "evaluation")
	private RelativeLayout evaluation;
	@ViewInject(id = R.id.setting_layout, click = "setting")
	private RelativeLayout setting;
	@ViewInject(id = R.id.layout_change_password, click = "onChangePassword")
	private RelativeLayout layout_change_password;

	@ViewInject(id = R.id.text_login_status)
	private TextView text_login_status;
	@ViewInject(id = R.id.text_login_info)
	private TextView text_login_info;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (contextView == null) {
			contextView = inflater.inflate(R.layout.fragment_activity_mine,
					null);
		}
		ViewGroup parent = (ViewGroup) contextView.getParent();
		if (parent != null) {
			parent.removeView(contextView);
		}
		FinalActivity.initInjectedView(this, contextView);
		initView();
		return contextView;
	}

	private void initView() {
		if (getLoginUser() != null) {
			text_login_info.setVisibility(View.VISIBLE);
			text_login_info.setText("你好，" + getLoginUser().getLoginname());
			text_login_status.setText("退出登录");
			layout_change_password.setVisibility(View.VISIBLE);
		} else {
			text_login_info.setVisibility(View.GONE);
			layout_change_password.setVisibility(View.GONE);
			text_login_status.setText("登录");
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initView();
	}

	// onClick
	// 登录我的评价用户资料设置
	public void login(View v) {
		if (getLoginUser() == null) {
			Intent intent = new Intent(contextView.getContext(),
					LoginActivity.class);
			startActivity(intent);
		} else {
			DialogUtils.confirmDialog("是否退出登录？", mActivity,
					new DialogCallBack() {
						@Override
						public void onPositiveButton(Object t) {
							MyApplication.instance().loginOut();
							Intent intent = new Intent(mActivity,
									LoginActivity.class);
							startActivity(intent);
						}
					});
		}
	}

	// 我的评价
	public void evaluation(View v) {
		if (getLoginUser() == null) {
			DialogUtils.alertDialog("您尚未登录，请先登录！", mActivity,
					new DialogCallBack() {
						@Override
						public void onNegativeButton(Object o) {
							Intent intent = new Intent(context,
									LoginActivity.class);
							startActivity(intent);
						}
					});
		} else {
			Intent intent = new Intent(contextView.getContext(),
					MineEvaluationActivity.class);
			startActivity(intent);
		}
	}

	// 用户资料设置
	public void onChangePassword(View v) {
		Intent intent = new Intent(contextView.getContext(),
				ChangePassActivity.class);
		startActivity(intent);
	}

	// 用户资料设置
	public void setting(View v) {
		CommonTools.showShortToast(context, "该功能暂未开放");
		// Intent intent = new Intent(contextView.getContext(),
		// SetInfoActivity.class);
		// startActivity(intent);
	}

}
